package edu.neusoft.kongfanfei317.yuejian1.activity;


import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import edu.neusoft.kongfanfei317.yuejian1.R;
import edu.neusoft.kongfanfei317.yuejian1.bean.LoginBean;
import edu.neusoft.kongfanfei317.yuejian1.listener.TListener;
import edu.neusoft.kongfanfei317.yuejian1.model.LoginModel;

public class LoginActivity extends BaseActivity {
    EditText et_user,et_password;
    Button btn_login,btn_register;
    TextView tv_yonghu;
    String username,userpass;
    CheckBox checkBox;
    Boolean remember;
    String user_id;
    TListener<LoginBean> tListener=new TListener<LoginBean>() {
        @Override
        public void onResponse(LoginBean loginbean) {
            if(loginbean.getUserid().equals("0")){
               // Toast.makeText(LoginActivity.this, "登录失败",Toast.LENGTH_SHORT).show();
                showToast("登录失败");
            }
            else{
                user_id= loginbean.getUserid();
                // Toast.makeText(LoginActivity.this, "登录成功",Toast.LENGTH_SHORT).show();
               showToast("登录成功");
                saveUser();
                Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                startActivityForResult(intent,1);
            }
        }
        @Override
        public void onFail(String msg) {
            //Toast.makeText(LoginActivity.this, "登录失败",Toast.LENGTH_SHORT).show();
            showToast("登录失败");
        }
    };
    void initView() {//模块绑定
        setLayout_file(R.layout.activity_login);
        et_user=(EditText)findViewById(R.id.et_user);
        et_password=(EditText)findViewById(R.id.et_password);
        btn_login=(Button)findViewById(R.id.btn_login);
        tv_yonghu = (TextView)findViewById(R.id.tv_yonghu);
        //btn_register=(Button)findViewById(R.id.btn_register);
        checkBox=(CheckBox)findViewById(R.id.checkBox);
        username=getUser_name();
        userpass=getUser_userpass();
        remember=getUser_remember();
        if(remember==true){
            et_user.setText(username);
            et_password.setText(userpass);
        }
       /* Intent intent = getIntent();
        String regname = intent.getStringExtra("username");
        String regpassword = intent.getStringExtra("userpass");
        et_user.setText(regname);
        et_password.setText(regpassword);*/
    }


    void initEvent() {
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(et_user.getText().toString().trim().equals("")||et_password.getText().toString().trim().equals("")){
                    showToast("不能为空");
                    //Toast.makeText(LoginActivity.this, "不能为空",Toast.LENGTH_SHORT).show();
                }
                else {
                    login();


//                    Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                    //把用户名和密码放到服务器上验证
                }
            }
        });
        tv_yonghu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
                finish();
            }
        });
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            }
        });

    }

    void initData() {

    }

    void login(){
        username=et_user.getText().toString();
        userpass=et_password.getText().toString();
        LoginModel loginModel=new LoginModel();
        loginModel.getResult(username,userpass,tListener);

    }
    public void saveUser(){
        SharedPreferences sp=getSharedPreferences("userInfo",MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("user_id",user_id);
        editor.putString("username",et_user.getText().toString());
        editor.putString("userpass",et_password.getText().toString());
        editor.putBoolean("remember",checkBox.isChecked());
        editor.commit();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode,resultCode,data);
        switch (requestCode){
            case 1:
                if (resultCode==RESULT_OK)
                {
                    String string=data.getStringExtra("username1");
                    String string1=data.getStringExtra("userpass1");
                    Toast.makeText(LoginActivity.this,string, Toast.LENGTH_SHORT).show();
                    et_user.setText(string);
                  et_password.setText(string1);
                }
        }
    }

}
